package chineseholiday.infrastructure.dao;

import chineseholiday.domain.holiday.Holiday;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 陈浩
 * @date 2021/5/17 10:45
 */
@Slf4j
public class HolidayDao {

    final String url = "http://timor.tech/api/holiday/year/";

    public HashMap<LocalDate, Holiday> getHoliday() throws Exception {
        ArrayList<Holiday> holidayList = getHolidayList();
        if (holidayList == null) {
            throw new Exception("查找节假日接口出错");
        }
        return new HashMap<>(holidayList.stream().collect(Collectors.toMap(Holiday::getDate, Function.identity())));

    }


    private ArrayList<Holiday> getHolidayList() {
        Map requestHoliday = requestHoliday();
        Integer code = (Integer)requestHoliday.get("code");
        ArrayList<Holiday> rawInfoList = new ArrayList<>();
        if (code == 0) {
            JSONObject holiday = (JSONObject)requestHoliday.get("holiday");
            Set<Map.Entry<String, Object>> entries = holiday.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                String key = entry.getKey();
                Object value = entry.getValue();
                Holiday object = JSONObject.parseObject(JSONObject.toJSONString(value), Holiday.class);
                rawInfoList.add(object);
            }
            return rawInfoList;
        }
        return null;
    }

    /**
     * 请求接口获取当年的节假日和补班数据
     * @return
     */
    private Map requestHoliday() {
        String rsa = null;
        try {
            for (int i1 = 0; i1 < 3; i1++) {
                rsa= HttpUtil.get(url);
                if (rsa != null) {
                    break;
                }
                Thread.sleep(1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (StrUtil.isBlank(rsa)) {
            log.warn("节假日接口有过期或被封IP的可能，请更新下面最新的节假日json或替换接口");
            return JSONObject.parseObject(json, Map.class);
        } else {
            return JSONObject.parseObject(rsa, Map.class);
        }
    }

    // 以免被外部节假日接口屏蔽了IP，获取不到今年节假日;
    private String json = "{\n" +
            "  \"code\": 0,\n" +
            "  \"holiday\": {\n" +
            "    \"01-01\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"元旦\",\n" +
            "      \"wage\": 3,\n" +
            "      \"date\": \"2021-01-01\"\n" +
            "    },\n" +
            "    \"01-02\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"元旦\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-01-02\"\n" +
            "    },\n" +
            "    \"01-03\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"元旦\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-01-03\"\n" +
            "    },\n" +
            "    \"02-07\": {\n" +
            "      \"holiday\": false,\n" +
            "      \"name\": \"春节前调休\",\n" +
            "      \"after\": false,\n" +
            "      \"wage\": 1,\n" +
            "      \"target\": \"春节\",\n" +
            "      \"date\": \"2021-02-07\"\n" +
            "    },\n" +
            "    \"02-11\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"除夕\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-02-11\"\n" +
            "    },\n" +
            "    \"02-12\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"初一\",\n" +
            "      \"wage\": 3,\n" +
            "      \"date\": \"2021-02-12\"\n" +
            "    },\n" +
            "    \"02-13\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"初二\",\n" +
            "      \"wage\": 3,\n" +
            "      \"date\": \"2021-02-13\"\n" +
            "    },\n" +
            "    \"02-14\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"初三\",\n" +
            "      \"wage\": 3,\n" +
            "      \"date\": \"2021-02-14\"\n" +
            "    },\n" +
            "    \"02-15\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"初四\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-02-15\"\n" +
            "    },\n" +
            "    \"02-16\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"初五\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-02-16\"\n" +
            "    },\n" +
            "    \"02-17\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"初六\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-02-17\"\n" +
            "    },\n" +
            "    \"02-20\": {\n" +
            "      \"holiday\": false,\n" +
            "      \"name\": \"春节后调休\",\n" +
            "      \"after\": true,\n" +
            "      \"wage\": 1,\n" +
            "      \"target\": \"春节\",\n" +
            "      \"date\": \"2021-02-20\"\n" +
            "    },\n" +
            "    \"04-03\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"清明节\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-04-03\"\n" +
            "    },\n" +
            "    \"04-04\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"清明节\",\n" +
            "      \"wage\": 3,\n" +
            "      \"date\": \"2021-04-04\"\n" +
            "    },\n" +
            "    \"04-05\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"清明节\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-04-05\"\n" +
            "    },\n" +
            "    \"04-25\": {\n" +
            "      \"holiday\": false,\n" +
            "      \"name\": \"劳动节前调休\",\n" +
            "      \"after\": false,\n" +
            "      \"wage\": 1,\n" +
            "      \"target\": \"劳动节\",\n" +
            "      \"date\": \"2021-04-25\"\n" +
            "    },\n" +
            "    \"05-01\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"劳动节\",\n" +
            "      \"wage\": 3,\n" +
            "      \"date\": \"2021-05-01\"\n" +
            "    },\n" +
            "    \"05-02\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"劳动节\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-05-02\"\n" +
            "    },\n" +
            "    \"05-03\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"劳动节\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-05-03\"\n" +
            "    },\n" +
            "    \"05-04\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"劳动节\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-05-04\"\n" +
            "    },\n" +
            "    \"05-05\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"劳动节\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-05-05\"\n" +
            "    },\n" +
            "    \"05-08\": {\n" +
            "      \"holiday\": false,\n" +
            "      \"name\": \"劳动节后调休\",\n" +
            "      \"after\": true,\n" +
            "      \"wage\": 1,\n" +
            "      \"target\": \"劳动节\",\n" +
            "      \"date\": \"2021-05-08\"\n" +
            "    },\n" +
            "    \"06-12\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"端午节\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-06-12\",\n" +
            "      \"rest\": 1\n" +
            "    },\n" +
            "    \"06-13\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"端午节\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-06-13\",\n" +
            "      \"rest\": 1\n" +
            "    },\n" +
            "    \"06-14\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"端午节\",\n" +
            "      \"wage\": 3,\n" +
            "      \"date\": \"2021-06-14\",\n" +
            "      \"rest\": 1\n" +
            "    },\n" +
            "    \"09-18\": {\n" +
            "      \"holiday\": false,\n" +
            "      \"after\": false,\n" +
            "      \"name\": \"中秋节前调休\",\n" +
            "      \"wage\": 1,\n" +
            "      \"target\": \"中秋节\",\n" +
            "      \"date\": \"2021-09-18\",\n" +
            "      \"rest\": 94\n" +
            "    },\n" +
            "    \"09-19\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"中秋节\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-09-19\",\n" +
            "      \"rest\": 95\n" +
            "    },\n" +
            "    \"09-20\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"中秋节\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-09-20\"\n" +
            "    },\n" +
            "    \"09-21\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"中秋节\",\n" +
            "      \"wage\": 3,\n" +
            "      \"date\": \"2021-09-21\"\n" +
            "    },\n" +
            "    \"09-26\": {\n" +
            "      \"holiday\": false,\n" +
            "      \"after\": false,\n" +
            "      \"name\": \"国庆节前调休\",\n" +
            "      \"wage\": 1,\n" +
            "      \"target\": \"国庆节\",\n" +
            "      \"date\": \"2021-09-26\"\n" +
            "    },\n" +
            "    \"10-01\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"国庆节\",\n" +
            "      \"wage\": 3,\n" +
            "      \"date\": \"2021-10-01\"\n" +
            "    },\n" +
            "    \"10-02\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"国庆节\",\n" +
            "      \"wage\": 3,\n" +
            "      \"date\": \"2021-10-02\"\n" +
            "    },\n" +
            "    \"10-03\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"国庆节\",\n" +
            "      \"wage\": 3,\n" +
            "      \"date\": \"2021-10-03\"\n" +
            "    },\n" +
            "    \"10-04\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"国庆节\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-10-04\"\n" +
            "    },\n" +
            "    \"10-05\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"国庆节\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-10-05\"\n" +
            "    },\n" +
            "    \"10-06\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"国庆节\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-10-06\"\n" +
            "    },\n" +
            "    \"10-07\": {\n" +
            "      \"holiday\": true,\n" +
            "      \"name\": \"国庆节\",\n" +
            "      \"wage\": 2,\n" +
            "      \"date\": \"2021-10-07\"\n" +
            "    },\n" +
            "    \"10-09\": {\n" +
            "      \"holiday\": false,\n" +
            "      \"name\": \"国庆节后调休\",\n" +
            "      \"after\": true,\n" +
            "      \"wage\": 1,\n" +
            "      \"target\": \"国庆节\",\n" +
            "      \"date\": \"2021-10-09\"\n" +
            "    }\n" +
            "  }\n" +
            "}";

}
